package com.leozhang.app;

import java.util.*;

public class StreamDemo {

    public void collectionToStream() {
        List<String> list = Arrays.asList("apple", "banana", "orange");
        System.out.println(list.stream().count());
        System.out.println(list.stream()
                .mapToInt(String::length)
                .sum());

        HashSet<String> w = new HashSet<>(Arrays.asList("It's a wonderful day for studying Java 8!".split(" ")));
        w.stream().map(x -> x + " ")
                .forEach(System.out::print);
        System.out.println();

        for (String s : w) {
            System.out.print(s + " ");
        }
        System.out.println();

        Map<String, Double> m = new HashMap<>();
        m.put("A", 1.0);
        m.put("B", 2.0);
        m.put("C", 3.0);
        m.entrySet().stream()
                .map(e -> e.getKey() + "=" + e.getValue())
                .forEach(System.out::println);
    }

    public static void main(String[] args) {
        StreamDemo streamDemo = new StreamDemo();
        streamDemo.collectionToStream();

        new Random(47)
                .ints(5, 20)
                .distinct()
                .limit(10)
                .sorted()
                .forEach(System.out::println);

        Random random = new Random(47);
        SortedSet<Integer> rints = new TreeSet<>();
        while (rints.size() < 10) {
            int r = random.nextInt(20);
            if (r < 5)
                continue;
            rints.add(r);
        }
        System.out.println(rints);
    }
}
